package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Exception.CustomException;
import com.itheima.Exception.CustomException;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;


    //分页查询+条件查询
    @GetMapping("/page")
    //参数以k:v格式转发，不用加注解
    public R<IPage> SelectPage(Integer page, Integer pageSize) {
        Page<Category> categoryPage = new Page<Category>(page, pageSize);
        Page<Category> ipage = categoryService.page(categoryPage, null);
        return R.success(ipage);
    }


    //添加菜品和套餐
    @PostMapping
    public R<String> add(@RequestBody Category category, HttpServletRequest request) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser((Long) request.getSession().getAttribute("employee"));
        category.setUpdateUser((Long) request.getSession().getAttribute("employee"));

        boolean flag = categoryService.save(category);
        if (flag) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }


    //删除菜品和套餐
    @DeleteMapping
    public R<String> delete(Long id) throws CustomException {
        //查询分类是否有菜品
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(wrapper);

        //查询分类是否有套餐
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(wrapper2);

        if (count != 0) {
            throw new CustomException("当前分类下有菜品，不能删除");
        } else if (count2 != 0) {
            throw new CustomException("当前分类下有套餐，不能删除");
        }
        categoryService.removeById(id);
        return R.success("删除成功");
    }

    //修改菜品分类
    @PutMapping
    public R update(@RequestBody Category category) {
        boolean flag = categoryService.updateById(category);
        if (flag) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    //查询菜品
    @GetMapping("/list")
    public R selectType(Integer type) {
        //前台首页也是调用这个方法，type==null
        LambdaQueryWrapper<Category> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.eq(type != null, Category::getType, type);
        List<Category> list = categoryService.list(Wrapper);
        return R.success(list);
    }


}
